import { parse } from 'url';

// mock tableListDataSource
let tableListDataSource = [
  {
    callNo: "XH2019040112",
    birthDay: "2009-04-24T16:00:00.000Z",
    desc: "15095210225",
    name: "曲丽丽",
    status: 3,
    updatedAt: "2019-04-12T16:30:11.000Z",
  },
  {
    callNo: "XH2019040111",
    birthDay: "2016-04-24T16:02:55.000Z",
    desc: "13895210227",
    name: "江林",
    status: 3,
    updatedAt: "2019-04-12T16:01:01.000Z",
  },
  {
    callNo: "XH2019040110",
    birthDay: "2017-11-05T14:44:22.000Z",
    desc: "15823215458",
    name: "建魁",
    status: 2,
    updatedAt: "2019-04-12T14:25:14.000Z",
  },
  {
    callNo: "XH2019040109",
    birthDay: "1990-01-23T11:20:10.000Z",
    desc: "15545842349",
    name: "雅诺",
    status: 2,
    updatedAt: "2019-04-12T11:14:20.000Z",
  },
  {
    callNo: "XH2019020118",
    birthDay: "2012-04-02T16:00:00.000Z",
    desc: "15232564888",
    name: "珂仪",
    status: 2,
    updatedAt: "2019-04-12T15:07:15.000Z",
  },
  {
    callNo: "XH2019020117",
    birthDay: "2010-12-02T16:00:00.000Z",
    desc: "15203156248",
    name: "高新里",
    status: 2,
    updatedAt: "2019-04-12T14:21:00.000Z",
  },
  {
    callNo: "XH2019020116",
    birthDay: "1980-02-24T16:00:00.000Z",
    desc: "15856489543",
    name: "浩翔",
    status: 2,
    updatedAt: "2019-04-12T17:22:02.000Z",
  },
  {
    callNo: "XH2019020115",
    birthDay: "2000-05-17T16:00:00.000Z",
    desc: "13412356489",
    name: "北健",
    status: 2,
    updatedAt: "2019-04-12T11:45:04.000Z",
  },
  {
    callNo: "XH2019020114",
    birthDay: "2013-05-21T16:00:00.000Z",
    desc: "15523564895",
    name: "钟志权",
    status: 1,
    updatedAt: "2019-04-11T17:30:20.000Z",
  },
  {
    callNo: "XH2019020113",
    birthDay: "2015-05-18T16:00:00.000Z",
    desc: "13656428549",
    name: "连忠明",
    status: 2,
    updatedAt: "2019-04-11T15:05:40.000Z",
  },
  {
    callNo: "XH2019020112",
    birthDay: "1992-04-15T16:00:00.000Z",
    desc: "15232594852",
    name: "和新力",
    status: 2,
    updatedAt: "2019-04-11T14:40:12.000Z",
  },
  {
    callNo: "XH2019020110",
    birthDay: "1998-05-21T16:00:00.000Z",
    desc: "135256334895",
    name: "程鹏",
    status: 2,
    updatedAt: "2019-04-11T12:12:45.000Z",
  },
  {
    callNo: "XH2019020109",
    birthDay: "1989-06-02T16:00:00.000Z",
    desc: "15232554529",
    name: "尔克里",
    status: 2,
    updatedAt: "2019-04-11T10:00:21.000Z",
  },
  {
    callNo: "XH2019020108",
    birthDay: "2015-08-02T16:00:00.000Z",
    desc: "15032655485",
    name: "张宁浩",
    status: 2,
    updatedAt: "2019-04-11T08:45:06.000Z",
  },
  {
    callNo: "XH2019020107",
    birthDay: "2001-11-25T16:00:00.000Z",
    desc: "13956489954",
    name: "李鹏",
    status: 2,
    updatedAt: "2019-04-10T16:10:45.000Z",
  },
  {
    callNo: "XH2019020106",
    birthDay: "2016-07-11T16:00:00.000Z",
    desc: "13745623259",
    name: "成杵",
    status: 2,
    updatedAt: "2019-04-10T13:21:14.000Z",
  },
  {
    callNo: "XH2019020105",
    birthDay: "2009-04-24T16:00:00.000Z",
    desc: "13845689245",
    name: "魏东升",
    status: 2,
    updatedAt: "2019-04-10T12:02:35.000Z",
  },
  {
    callNo: "XH2019020104",
    birthDay: "2010-04-16T16:00:00.000Z",
    desc: "15203265478",
    name: "尚姗姗",
    status: 2,
    updatedAt: "2019-04-11T11:21:00.000Z",
  },
  {
    callNo: "XH2019013119",
    birthDay: "2004-06-11T16:00:00.000Z",
    desc: "15823021259",
    name: "刘泓渊",
    status: 2,
    updatedAt: "2019-04-11T11:01:47.000Z",
  },
  {
    callNo: "XH2019013118",
    birthDay: "2002-07-18T16:00:00.000Z",
    desc: "15032654785",
    name: "何敬东",
    status: 2,
    updatedAt: "2019-04-09T19:35:22.000Z",
  },
  
];
// for (let i = 0; i < 46; i += 1) {
//   tableListDataSource.push({
//     key: i,
//     disabled: i % 6 === 0,
//     href: 'https://ant.design',
//     avatar: [
//       'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
//       'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
//     ][i % 2],
//     name: `TradeCode ${i}`,
//     title: `一个任务名称 ${i}`,
//     owner: '曲丽丽',
//     desc: '这是一段描述',
//     callNo: Math.floor(Math.random() * 1000),
//     status: Math.floor(Math.random() * 10) % 4,
//     updatedAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
//     createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
//     progress: Math.ceil(Math.random() * 100),
//   });
// }

function getRule(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const params = parse(url, true).query;

  let dataSource = tableListDataSource;

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
      );
    });
    dataSource = filterDataSource;
  }

  if (params.name) {
    dataSource = dataSource.filter(data => data.name.indexOf(params.name) > -1);
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  return res.json(result);
}

function postRule(req, res, u, b) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const body = (b && b.body) || req.body;
  const { method, name, desc, key } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      tableListDataSource = tableListDataSource.filter(item => key.indexOf(item.key) === -1);
      break;
    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        href: 'https://ant.design',
        avatar: [
          'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
          'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
        ][i % 2],
        name: `TradeCode ${i}`,
        title: `一个任务名称 ${i}`,
        owner: '曲丽丽',
        desc,
        callNo: Math.floor(Math.random() * 1000),
        status: Math.floor(Math.random() * 10) % 2,
        updatedAt: new Date(),
        createdAt: new Date(),
        progress: Math.ceil(Math.random() * 100),
      });
      break;
    case 'update':
      tableListDataSource = tableListDataSource.map(item => {
        if (item.key === key) {
          Object.assign(item, { desc, name });
          return item;
        }
        return item;
      });
      break;
    default:
      break;
  }

  return getRule(req, res, u);
}

export default {
  'GET /api/rule': getRule,
  'POST /api/rule': postRule,
};
